package com.jt.controller;

import com.jt.pojo.Dept;
import com.jt.pojo.Result;
import com.jt.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 获取部门和部门下children信息
     */
    @GetMapping("/getMenuDeptList")
    public Result getDeptList() {
        List<Dept> allDeptList = deptService.getDeptList();
        List<Dept> oneDeptList = allDeptList.stream().filter(a -> a.getParentId().equals("1")).collect(Collectors.toList());
        oneDeptList.forEach(item -> {
            List<Dept> childrenList = allDeptList.stream().filter(a -> a.getParentId().equals(item.getId())).collect(Collectors.toList());
            item.setChildren(childrenList);
            getChildrenList(allDeptList, childrenList);
        });
        return new Result().setCode(200).setMassage("获取部门信息").setData(oneDeptList);
    }

    public void getChildrenList(List<Dept> allDeptList, List<Dept> childrenList) {
        childrenList.forEach(item -> {
            List<Dept> children = allDeptList.stream().filter(a -> a.getParentId().equals(item.getId())).collect(Collectors.toList());
            item.setChildren(children);
            getChildrenList(allDeptList, children);
        });
    }

    /**
     * 获取全部部门信息
     */
    @GetMapping("/allDeptList")
    public Result getAllDeptList() {
        List<Dept> allDeptList = deptService.getDeptList();
        return new Result().setCode(200).setMassage("获取部门信息").setData(allDeptList);
    }
}
